package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.UserDao;
import com.aigou.admin.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/11/29 09:19
 * @Version: 1.0
 */

@Repository
public class UserDaoImpl implements UserDao {
    /**
     * 查询所有的用户
     *
     * @return 用户列表
     */
    @Override
    public List<User> selectAll() {
        return JDBCUtils.executeQuery(User.class, "select * from s_user");
    }

    @Override
    public User selectById(String id) {
        List<User> users = JDBCUtils.executeQuery(User.class, "select * from s_user where user_id = ? ",  id);
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public Boolean insert(User user) {
        int rows = JDBCUtils.executeUpdate("insert into s_user(user_id,user_name,user_pwd,user_type) values(?,?,?,?)", user.getUser_id(), user.getUser_name(), user.getUser_pwd(), user.getUser_type());
        return rows > 0;
    }

    @Override
    public Boolean update(User user) {
        int rows = JDBCUtils.executeUpdate("update s_user set user_name =?,user_pwd=?,user_type=? where user_id = ?", user.getUser_name(), user.getUser_pwd(), user.getUser_type(),user.getUser_id());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_user where user_id = ?", id);
        return rows >0;
    }

    @Override
    public User selectByUsername(String username) {
        List<User> users = JDBCUtils.executeQuery(User.class, "select * from s_user where user_name = ? ",  username);
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public Integer selectCount(String username) {
        if (username != null) {
            return JDBCUtils.executeCount("select count(*) from s_user where user_name = ?", username);
        }
        return JDBCUtils.executeCount("select count(*) from s_user");

    }

    @Override
    public List<User> selectUserByLimit(Integer start, Integer limit, String username) {
        if (username != null) {
            return JDBCUtils.executeQuery(User.class, "select * from s_user where user_name like ? limit ?,?","%"+username+"%" ,start, limit);
        }
        return JDBCUtils.executeQuery(User.class, "select * from s_user limit ?,?", start, limit);
    }
}
